# %%
# -*- coding: UTF-8 -*-
#开始模拟数据
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# 用来绘图的，封装了matplot
# 要注意的是一旦导入了seaborn，
# matplotlib的默认作图风格就会被覆盖成seaborn的格式
import seaborn as sns       

from scipy import stats
from scipy.stats import  norm
from sklearn.preprocessing import StandardScaler
import warnings
warnings.filterwarnings('ignore')
%matplotlib inline  



data_train = pd.read_csv("F:\\linjingfeng\\outpath\\all\\part-00000-f606ec6c-8a94-42e6-ab29-22eb4c9d0cd4-c000.csv")
data_train
data_train['2021-02'].describe()
sns.distplot(data_train['2021-02'])
print("Skewness: %f" % data_train['2021-02'].skew())
print("Kurtosis: %f" % data_train['2021-02'].kurt())
# %%

# %%

var = '2020-10'
data = pd.concat([data_train['2021-02'], data_train[var]], axis=1)
fig = sns.boxplot(x=var, y="2021-02", data=data)
fig.axis(ymin=0, ymax=800000);
# %%


# %%

var = '2020-11'
data = pd.concat([data_train['2021-02'], data_train[var]], axis=1)
fig = sns.boxplot(x=var, y="2021-02", data=data)
fig.axis(ymin=0, ymax=800000);
# %%
# %%

var = '2020-12'
data = pd.concat([data_train['2021-02'], data_train[var]], axis=1)
fig = sns.boxplot(x=var, y="2021-02", data=data)
fig.axis(ymin=0, ymax=800000);
# %%


# %%

var = '2021-01'
data = pd.concat([data_train['2021-02'], data_train[var]], axis=1)
fig = sns.boxplot(x=var, y="2021-02", data=data)
fig.axis(ymin=0, ymax=800000);
# %%


# %%
from sklearn import preprocessing
from sklearn import linear_model, svm, gaussian_process
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection  import train_test_split
import numpy as np


cols = ['2020-10','2020-11','2020-12','2021-01']
x = data_train[cols].values
y = data_train['2021-02'].values
x_scaled = preprocessing.StandardScaler().fit_transform(x)
y_scaled = preprocessing.StandardScaler().fit_transform(y.reshape(-1,1))
X_train,X_test,y_train,y_test = train_test_split(x_scaled,y_scaled,test_size=0.33,random_state=42)
clfs = {
        'svm':svm.SVR(), 
        'RandomForestRegressor':RandomForestRegressor(n_estimators=400),
        'BayesianRidge':linear_model.BayesianRidge()
       }
for clf in clfs:
    try:
        clfs[clf].fit(X_train, y_train)
        y_pred = clfs[clf].predict(X_test)
        print(clf + " cost:" + str(np.sum(y_pred-y_test)/len(y_pred)) )
    except Exception as e:
        print(clf + " Error:")
        print(str(e))

cols = ['2020-10','2020-11','2020-12','2021-01']
x = data_train[cols].values
y = data_train['2021-02'].values
X_train,X_test, y_train, y_test = train_test_split(x, y, test_size=0.33, random_state=42)

clf = RandomForestRegressor(n_estimators=400)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
print(y_pred)
# %%

# %%
y_test
sum(abs(y_pred - y_test))/len(y_pred)

# %%

# %%
import pandas as pd
from sklearn.ensemble import RandomForestRegressor

# 之前训练的模型
rfr = clf


data_test = pd.read_csv("F:\\linjingfeng\\outpath\\test\\part-00000-d44bce2a-6dfe-40ab-a0e0-d9870a0713f9-c000.csv")

data_test

cols = ['2020-10','2020-11','2020-12','2021-01']
x = data_test[cols].values
y_te_pred = rfr.predict(x)

prediction = pd.DataFrame(y_te_pred, columns=['2021-02'])
result = pd.concat([ data_test['title'], prediction], axis=1)
result.columns

result



# %%
